import {getAll, WebviewWindow} from '@tauri-apps/api/window'

/**
 * 显示帮助窗口
 */
export async function show_help_window(): Promise<void> {
    await show_window('help', '/help')
}

/**
 * 显示设置窗口
 */
export async function show_setting_window(): Promise<void> {
    await show_window('setting', '/setting')
}

/**
 * 显示组件-V2ray窗口
 */
export async function show_component_v2ray_window(): Promise<void> {
    await show_window('v2ray', '/component/v2ray')
}

/**
 * 显示组件-Color窗口
 */
export async function show_component_color_window(): Promise<void> {
    await show_window('color', '/component/color')
}

/**
 * 显示窗口
 * @param label
 * @param route
 * @param min_width
 * @param min_height
 */
async function show_window(label: string, route: string, min_width: number = 800, min_height: number = 600): Promise<void> {
    const all_windows = getAll()

    let has_window = false
    for (const window of all_windows) {
        if (label === window.label) {
            has_window = true
            await window.show()
            break
        }
    }

    if (!has_window) {
        const window = new WebviewWindow(label, {
            url: route,
            decorations: false,
            minWidth: min_width,
            minHeight: min_height,
            width: 1280,
            height: 900,
        })

        await window.show()
    }
}
